home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / NeuroSim 1.0 / .cp / NS_Utils.cp < prev   
Text File  |  1996-02-19  |  4KB  |  125 lines

  1. // ===========================================================================
  2. //    NS_Utils.cp                    ©1996 Timo Eloranta
  3. // ===========================================================================
  4. //    Utility functions used by the NeuroSim app
  5.  
  6. #include "NS_Utils.h"
  7. #include <Sound.h>        // Universal header - SndPlay() etc.
  8.  
  9. // ---------------------------------------------------------------------------
  10. //        • RangedRdm
  11. //
  12. //          Called by:    CNeuralNet::GenerateConnections
  13. //                        CNeuralNet::GetRandomReceptor
  14. // ---------------------------------------------------------------------------
  15. //    Return a random short integer from the range [inMin, inMax].
  16.  
  17. Int16 
  18. RangedRdm( Int16 inMin, Int16 inMax)
  19. {
  20.     Int16    theRange = (inMax - inMin) + 1;
  21.     Int32    theRaw = ::Random();
  22.     
  23.     if ( theRaw < 0L )
  24.         theRaw *= -1L;
  25.         
  26.     theRaw = ( theRaw * (Int32) theRange ) / ( (Int32) max_Int16 + 1L );
  27.     
  28.     return    inMin + (Int16) theRaw;
  29. }
  30.  
  31. // ---------------------------------------------------------------------------
  32. //        • PlayOneSnd
  33. //
  34. //          Called by:    CNeuron::IncState
  35. //                        CStdNeuron::DoClickAction
  36. // ---------------------------------------------------------------------------
  37. //    Play a single sound from a snd resource. If the Boolean inAsync 
  38. //    parameter is true, the sound is played asynchronously.
  39.  
  40. pascal OSErr
  41. PlayOneSnd ( ResIDT inSoundID, Boolean inAsync)
  42. {
  43.             OSErr           theOE = noErr;
  44.  
  45.     static  SndListHandle   sPlayingSound;
  46.     static  SndChannelPtr   sCurChannel;
  47.  
  48.     if ( sCurChannel ) {
  49.         if (!( theOE = ::SndDisposeChannel( sCurChannel, true )))
  50.             sCurChannel = nil;
  51.     }
  52.  
  53.     if ( sPlayingSound ) {
  54.         OSErr theOE2;
  55.         ::ReleaseResource( (Handle) sPlayingSound );
  56.         if ( !( theOE2 = ::ResError() ))
  57.             sPlayingSound = nil;
  58.         else if ( !theOE )
  59.             theOE = theOE2;
  60.     }
  61.  
  62.     if ( !theOE ) {
  63.         sPlayingSound = (SndListHandle) ::GetResource('snd ', inSoundID );
  64.         
  65.         if ( !( theOE = ::ResError() ))
  66.             if ( !sPlayingSound ) 
  67.                 theOE = resNotFound;
  68.             else {
  69.                 ::HLockHi( (Handle) sPlayingSound );
  70.             
  71.                 if ( !inAsync ) {
  72.                     if (!(theOE = ::SndPlay( nil, sPlayingSound, false ))) {
  73.                         ::ReleaseResource( (Handle) sPlayingSound );
  74.                         if ( !theOE ) 
  75.                             theOE = ::ResError ( );
  76.                         if ( !theOE ) 
  77.                             sPlayingSound = nil;
  78.                     }
  79.                 }
  80.                 else if (!(theOE = ::SndNewChannel( &sCurChannel,
  81.                                         sampledSynth, initMono, nil)))
  82.                                         
  83.                     theOE = ::SndPlay( sCurChannel, sPlayingSound, true );
  84.             }
  85.     }
  86.     return theOE;
  87. }
  88.  
  89. // ---------------------------------------------------------------------------
  90. //        • SignedIntField
  91. //
  92. //          Called by:    CParamsDialog::InitDialog
  93. // ---------------------------------------------------------------------------
  94. //    Key Filter for Integer characters plus the '-' character
  95. //
  96. //        > Identify delete and cursor keys
  97. //        > Accept the '-' character
  98. //        > Accept numbers (0 to 9)
  99. //        > Reject all other printing characters
  100. //        > PassUp all other characters
  101.  
  102. EKeyStatus
  103. SignedIntField( const EventRecord &inKeyEvent)
  104. {
  105.     EKeyStatus    theKeyStatus = keyStatus_PassUp;
  106.     Char16        theKey = inKeyEvent.message;
  107.     Char16        theChar = theKey & charCodeMask;
  108.     
  109.     if (UKeyFilters::IsTEDeleteKey(theKey)) {
  110.         theKeyStatus = keyStatus_TEDelete;
  111.     } else if (UKeyFilters::IsTECursorKey(theKey)) {
  112.         theKeyStatus = keyStatus_TECursor;
  113.     } else if (UKeyFilters::IsExtraEditKey(theKey)) {
  114.         theKeyStatus = keyStatus_ExtraEdit;
  115.     } else if (UKeyFilters::IsPrintingChar(theChar)) {
  116.         if (UKeyFilters::IsNumberChar(theChar) || theChar == '-') {
  117.             theKeyStatus = keyStatus_Input;
  118.         } else {
  119.             theKeyStatus = keyStatus_Reject;
  120.         }
  121.     }
  122.     
  123.     return theKeyStatus;
  124. }
  125.